/**
 * @copyright Copyright (c) 2025 CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author Chen Yongjie
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <CUnit/Basic.h>
#include <stdint.h> 
#include "test_cpu.h"
#include "test_cs_getopt.h"  
#include "test_unit.h"       
#include "test_uart.h"

int main() {
    // 初始化 CUnit 测试套件
    if (CUE_SUCCESS != CU_initialize_registry()) {
        return CU_get_error();
    }
    CU_pSuite pSuite1 = CU_add_suite("suite_1", NULL, NULL);
    CU_pSuite pSuite2 = CU_add_suite("suite_2", NULL, NULL);
    if (NULL == pSuite1 || NULL == pSuite2) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    // 设置自动化模式的输出文件名
    CU_set_output_filename("CUnit");

    // 运行自动化测试
    CU_automated_run_tests();

    // 添加 CPU 测试套件
    CU_pSuite pSuite3 = CU_add_suite("CPU_Test_Suite", NULL, NULL);
    if (NULL == pSuite3) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    // 添加 CPU 测试用例
    if ((NULL == CU_add_test(pSuite3, "test_get_cpu_info_normal", test_get_cpu_info_normal)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpu_info_file_not_exist", test_get_cpu_info_file_not_exist)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpu_info_null_pointer", test_get_cpu_info_null_pointer)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_normal", test_cal_cpu_used_rate_normal)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_same_time", test_cal_cpu_used_rate_same_time)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_idle_increase", test_cal_cpu_used_rate_idle_increase)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_user_increase", test_cal_cpu_used_rate_user_increase)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpuUsage_normal", test_get_cpuUsage_normal)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpuUsage_continuous", test_get_cpuUsage_continuous)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_system_increase", test_cal_cpu_used_rate_system_increase)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_proportional_increase", test_cal_cpu_used_rate_proportional_increase)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_overflow", test_cal_cpu_used_rate_overflow)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_null_pointer", test_cal_cpu_used_rate_null_pointer)) ||
        // 添加缺失的CPU测试用例
        (NULL == CU_add_test(pSuite3, "test_get_cpu_info_hotplug", test_get_cpu_info_hotplug)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpu_info_freq_scaling", test_get_cpu_info_freq_scaling)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_extreme_usage", test_cal_cpu_used_rate_extreme_usage)) ||
        (NULL == CU_add_test(pSuite3, "test_cal_cpu_used_rate_rapid_changes", test_cal_cpu_used_rate_rapid_changes)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpuUsage_load_spike", test_get_cpuUsage_load_spike)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpuUsage_high_load", test_get_cpuUsage_high_load)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpuUsage_low_load", test_get_cpuUsage_low_load)) ||
        (NULL == CU_add_test(pSuite3, "test_get_cpuUsage_freq_change", test_get_cpuUsage_freq_change)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    // 添加 Docker 相关测试用例
    if ((NULL == CU_add_test(pSuite3, "test_cpu_scanDocker_normal", test_cpu_scanDocker_normal)) ||
        (NULL == CU_add_test(pSuite3, "test_cpu_scanDocker_no_containers", test_cpu_scanDocker_no_containers)) ||
        (NULL == CU_add_test(pSuite3, "test_cpu_scanDocker_daemon_not_running", test_cpu_scanDocker_daemon_not_running)) ||
        (NULL == CU_add_test(pSuite3, "test_cpu_scanDocker_memory_allocation_failure", test_cpu_scanDocker_memory_allocation_failure)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    // 添加 Flash 使用率相关测试用例
    if ((NULL == CU_add_test(pSuite3, "test_get_flashUsage_normal", test_get_flashUsage_normal)) ||
        (NULL == CU_add_test(pSuite3, "test_get_flashUsage_command_fail", test_get_flashUsage_command_fail)) ||
        (NULL == CU_add_test(pSuite3, "test_get_flashUsage_null_parameter", test_get_flashUsage_null_parameter)) ||
        (NULL == CU_add_test(pSuite3, "test_get_flashUsage_unmounted", test_get_flashUsage_unmounted)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    // 添加内存使用率测试用例
    if ((NULL == CU_add_test(pSuite3, "test_get_memUsage_normal", test_get_memUsage_normal)) ||
        (NULL == CU_add_test(pSuite3, "test_get_memUsage_file_not_exist", test_get_memUsage_file_not_exist)) ||
        (NULL == CU_add_test(pSuite3, "test_get_memUsage_invalid_format", test_get_memUsage_invalid_format)) ||
        (NULL == CU_add_test(pSuite3, "test_get_memUsage_invalid_values", test_get_memUsage_invalid_values)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }

    // 添加 cs_getopt 测试套件
    CU_pSuite pSuite_getopt = CU_add_suite("CS_GETOPT_Test_Suite", NULL, NULL);
    if (NULL == pSuite_getopt) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    // 添加 cs_getopt 测试用例
    if ((NULL == CU_add_test(pSuite_getopt, "test_help_output", test_help_output)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_help_stdout_failure", test_help_stdout_failure)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_help_format", test_help_format)) ||
        // 添加缺失的cs_getopt测试用例
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_version", test_getOptionParam_version)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_cpu", test_getOptionParam_cpu)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_memory", test_getOptionParam_memory)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_storage", test_getOptionParam_storage)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_all", test_getOptionParam_all)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_invalid", test_getOptionParam_invalid)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_no_args", test_getOptionParam_no_args)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_empty_param", test_getOptionParam_empty_param)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_combinations", test_getOptionParam_combinations)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_syscall_failure", test_getOptionParam_syscall_failure)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_boundary_conditions", test_getOptionParam_boundary_conditions)) ||
        (NULL == CU_add_test(pSuite_getopt, "test_getOptionParam_state_cleanup", test_getOptionParam_state_cleanup)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    // 添加Unit测试套件
    CU_pSuite pSuite_unit = CU_add_suite("UNIT_Test_Suite", NULL, NULL);
    if (NULL == pSuite_unit) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    // 添加Unit测试用例
    if ((NULL == CU_add_test(pSuite_unit, "test_get_token_normal", test_get_token_normal)) ||
        (NULL == CU_add_test(pSuite_unit, "test_get_token_max_value", test_get_token_max_value)) ||
        (NULL == CU_add_test(pSuite_unit, "test_get_token_null_pointer", test_get_token_null_pointer)) ||
        (NULL == CU_add_test(pSuite_unit, "test_get_system_time_us_normal", test_get_system_time_us_normal)) ||
        (NULL == CU_add_test(pSuite_unit, "test_get_system_time_absoffset_normal", test_get_system_time_absoffset_normal)) ||
        (NULL == CU_add_test(pSuite_unit, "test_get_system_time_absoffset_overflow", test_get_system_time_absoffset_overflow)) ||
        (NULL == CU_add_test(pSuite_unit, "test_AtoH_normal", test_AtoH_normal)) ||
        (NULL == CU_add_test(pSuite_unit, "test_AtoH_null_pointer", test_AtoH_null_pointer)) ||
        (NULL == CU_add_test(pSuite_unit, "test_AtoH_empty_string", test_AtoH_empty_string)) ||
        (NULL == CU_add_test(pSuite_unit, "test_AtoH_odd_length", test_AtoH_odd_length)) ||
        (NULL == CU_add_test(pSuite_unit, "test_mqtt_get_time_normal", test_mqtt_get_time_normal)) ||
        (NULL == CU_add_test(pSuite_unit, "test_mqtt_get_time_null_pointer", test_mqtt_get_time_null_pointer)) ||
        (NULL == CU_add_test(pSuite_unit, "test_delay_ms_normal", test_delay_ms_normal)) ||
        (NULL == CU_add_test(pSuite_unit, "test_delay_ms_various_delays", test_delay_ms_various_delays)) ||
        (NULL == CU_add_test(pSuite_unit, "test_get_current_time_global_normal", test_get_current_time_global_normal)) ||
        (NULL == CU_add_test(pSuite_unit, "test_get_current_time_global_null_pointer", test_get_current_time_global_null_pointer)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    // 添加UART测试套件
    CU_pSuite pSuite_uart = CU_add_suite("UART_Test_Suite", NULL, NULL);
    if (NULL == pSuite_uart) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    // 添加UART测试用例
    if ((NULL == CU_add_test(pSuite_uart, "test_set_opt_normal", test_set_opt_normal)) ||
        (NULL == CU_add_test(pSuite_uart, "test_set_opt_invalid_fd", test_set_opt_invalid_fd)) ||
        (NULL == CU_add_test(pSuite_uart, "test_set_opt_invalid_baudrate", test_set_opt_invalid_baudrate)) ||
        (NULL == CU_add_test(pSuite_uart, "test_set_opt_invalid_wordlength", test_set_opt_invalid_wordlength)) ||
        (NULL == CU_add_test(pSuite_uart, "test_set_opt_invalid_parity", test_set_opt_invalid_parity)) ||
        (NULL == CU_add_test(pSuite_uart, "test_set_opt_invalid_stopbit", test_set_opt_invalid_stopbit)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_init_normal", test_uart_init_normal)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_init_invalid_port", test_uart_init_invalid_port)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_init_invalid_baudrate", test_uart_init_invalid_baudrate)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_init_invalid_wordlength", test_uart_init_invalid_wordlength)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_init_invalid_parity", test_uart_init_invalid_parity)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_init_invalid_stopbit", test_uart_init_invalid_stopbit)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_init_null_port", test_uart_init_null_port)) ||  // 新增测试用例
        (NULL == CU_add_test(pSuite_uart, "test_serial_send_normal", test_serial_send_normal)) ||
        (NULL == CU_add_test(pSuite_uart, "test_serial_send_null_buffer", test_serial_send_null_buffer)) ||
        (NULL == CU_add_test(pSuite_uart, "test_serial_send_zero_length", test_serial_send_zero_length)) ||
        (NULL == CU_add_test(pSuite_uart, "test_serial_send_invalid_fd", test_serial_send_invalid_fd)) ||
        // 添加缺失的UART测试用例
        (NULL == CU_add_test(pSuite_uart, "test_serial_recv_normal", test_serial_recv_normal)) ||
        (NULL == CU_add_test(pSuite_uart, "test_serial_recv_null_buffer", test_serial_recv_null_buffer)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_destory_multiple_calls", test_uart_destory_multiple_calls)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_high_frequency_operations", test_uart_high_frequency_operations)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_different_baudrates", test_uart_different_baudrates)) ||
        (NULL == CU_add_test(pSuite_uart, "test_uart_different_parity", test_uart_different_parity)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }

    CU_cleanup_registry();
    return CU_get_error();

        // 运行测试用例--测试时用
    //CU_set_test_name("CUnit");
    //CU_basic_run_tests();


}